//index.js
//获取应用实例
var app = getApp()
let httpUrl = app.getHttpUrl();
Page({
  data: {
    friendData: [],
    downBlock: true,
    page: 1,
    isLoad: true,
  },
  onLoad: function () {
    this.loadData();
  },
  loadData: function () {
    if (wx.showLoading) {
      wx.showLoading({
        title: '正在加载...',
      })
    }
    const page = this.data.page;
    wx.request({
      url: httpUrl + 'slsw/showfriends.do',
      method: 'POST',
      data: `weChatKey=9237B361957490BA9644A710A5E3DD84&&Data={is_refresh:'${page == 1}',page_num:'${page}',item_num:10}`,
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=UTF-8"'
      },
      success: (res) => {
        let list = res.data.Data;
        let isLoad = (list.length == 10) ? true : false;
        this.setData({
          friendData: page == 1 ? list : [...this.data.friendData, ...list],
          isLoad: isLoad
        })
        wx.hideLoading();
      },
      fail: () => {
        wx.hideLoading();
        wx.showToast({
          title: '服务器开了个小差，请稍后再试!',
          icon: 'loading'
        })
      }
    })
  },
  // 上拉加载
  // onReachBottom
  onReachBottom: function (e) {
    let isLoad = this.data.isLoad;
    if (isLoad == true) {
      this.setData({
        page: this.data.page + 1
      })
      this.loadData();
    } else {
      wx.hideLoading();
      wx.showToast({
        title: '已经没有更多数据了!',
        icon: 'clear'
      })
    }
  },
  //获取不到头像图片时采用默认图片
  errImg: function (e) {
    var _errImg = e.target.dataset.errImg;
    var _objImg = "'" + _errImg + "'";
    var _errObj = {};
    _errObj[_errImg] = "../../image/attr.png";
    this.setData(_errObj);//注意这里的赋值方式...
  },
  // 首页图片预览
  preimage: function (e) {
    let current = e.target.dataset.src;
    let index = e.target.dataset.index;
    let photos = this.data.friendData[index].photos;
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: photos // 需要预览的图片http链接列表  
    })
  },
  // 荒料搜索
  entrancebm: function (e) {
    wx.navigateTo({
      url: '../search-material/search-material?searchPageType=1',
    })
  },
  // 大阪搜索
  entrancesl: function (e) {
    wx.navigateTo({
      url: '../search-material/search-material?searchPageType=2',
    })
  },
  downClose: function (e) {
    this.setData({
      downBlock: false,
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
